<template>
	<view>
		<view class="PolicySearch Enterprise">
			<view class="PolicySearchc">
				<input type="text" v-model="searchKeyword" placeholder="输入搜索关键词">
				<view class="PolocyBtn">搜索</view>
			</view>
		</view>
		<view class="MatchReportData">
			<view class="MatchReportLi" v-for="item in list" :key="item.id">
				<view class="MatchReportLis" :style="{paddingRight: item.status == 1 || item.status == 3 ? '0' : '140rpx'}" @click="goToDetail(item)" style="position: initial;">
					<view class="MatchReportLilTitle">{{item.policyName}}</view>
					<view class="MatchReportLilTime" v-if="item.status == 2">生成时间：{{item.createTime}}</view>
					<view class="MatchReportLisFixed" v-if="item.status == 2">
						<view class="MatchReportLisFixedCount">{{ item.matchPolicy.score }}</view>
						<view class="MatchReportLisFixedText">匹配分值</view>
					</view>
				</view>
				<view class="MatchReportLix">
					<view v-if="item.status == 1" class="MatchReportLixText" @click="goToDetail(item)">报告生成中...</view>
					<view style="color: #ff2200;" v-if="item.status == 3" class="MatchReportLixText">生成失败</view>
					<view style="margin-left: auto;" v-if="item.status != 1 && item.status != 3" class="MatchReportLixBtn" @click="handleDelete(item.id)">删除报告</view>
					<view style="margin-left: auto;" v-if="item.status == 3" class="MatchReportLixBtn" @click="resetGetData(item.companyId, item.policyId)">重新生成</view>
				</view>
			</view>
		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import selectFilter from '@/pages/components/selectFilter.vue';
	import { getMyReportList, deleteMyReport, matchPolicy } from '@/api/api.js';
	export default {
		components: {
			selectFilter
		},
		data() {
			return {
				shaiXuanType: false,
				list: [],
				searchKeyword: '',
				messageText: '报告正在生成中，请稍等',
				msgType: 'warn',
			};
		},
		created() {
			this.getList();
		},
		methods: {
			resetGetData(companyId, policyId) {
				this.msgType = 'warn'
				this.messageText = '报告重新生成中，请稍后'
				this.$refs.message.open();
				matchPolicy(companyId, policyId).then();
				setTimeout(() => {
					this.getList()
				}, 1000)
			},
			async getList() {
				try {
					const res = await getMyReportList({ policyName: this.searchKeyword });
					console.log('报告列表数据：', res);
					if (res.code === 0) {
						this.list = res.data?.list || [];
					} else {
						console.error('获取报告列表失败：', res.message);
					}
				} catch (error) {
					console.error('获取报告列表失败：', error);
				}
			},
			async handleDelete(id) {
				try {
					const { confirm } = await uni.showModal({
						title: '提示',
						content: '确定要删除该报告吗？'
					});
					if (!confirm) return;
					const res = await deleteMyReport(id);
					if (res.code === 0) {
						uni.showToast({
							title: '删除成功',
							icon: 'success'
						});
						await this.getList();
					} else {
						uni.showToast({
							title: res.message || '删除失败',
							icon: 'error'
						});
					}
				} catch (error) {
					console.error('删除报告失败：', error);
					uni.showToast({
						title: '删除失败',
						icon: 'error'
					});
				}
			},
			goToDetail(item) {
				if (item.status == 3) {
					this.msgType = 'error'
					this.messageText = '报告生成失败，请重新生成！'
					this.$refs.message.open();
				}
				if (item.status == 2) {
					const reportData = encodeURIComponent(JSON.stringify(item));
					uni.navigateTo({
						url: `/pages/matchReport/details/index?reportData=${reportData}`
					});
				} else {
					this.$refs.message.open();
				}
			}
		},
		watch: {
			searchKeyword: {
				handler(newVal) {
					this.getList();
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f7f8fc;
	}

	.Enterprise {
		padding: 30rpx 24rpx 40rpx;
		background: none;
	}


	.MatchReportData {
		padding: 10rpx 24rpx 0;
		.MatchReportLi {
			padding: 28rpx 31rpx 12rpx;
			border-radius: 20rpx;
			margin: 0 0 30rpx;
			background: #ffffff;
			position: relative;
			.MatchReportLis{
				padding-right: 140rpx;
				cursor: pointer;
				position: relative;
				z-index: 99;
				.MatchReportLilTitle{
					font-size: 32rpx;
					color: #202020;
					padding: 0 0 20rpx;
				}
				.MatchReportLilTime{
					font-size: 28rpx;
					color: #4e5262;
					padding-left: 43rpx;
					background: url("/static/images/timeimg.png") no-repeat left center / 28rpx 29rpx;
				}
				.MatchReportLisFixed{
					position: absolute;
					right: 0;
					top: 40rpx;
					width: 118rpx;
					height: 90rpx;
					text-align: center;
					background: url("/static/images/reportbj.png") no-repeat center / 118rpx 90rpx;
					.MatchReportLisFixedCount{
						font-size: 26rpx;
						color: #ffffff;
						padding: 10rpx 0 0;
					}
					.MatchReportLisFixedText{
						font-size: 24rpx;
						color: #ffffff;
					}
				}
			}
			.MatchReportLix{
				display: flex;
				justify-content: space-between;
				padding: 19rpx 0;
				margin: 20rpx 0 0;
				border-top: 1px solid #f1f1f1;
				text-align: right;
				.MatchReportLixBtn{
					padding: 0 29rpx;
					height: 60rpx;
					line-height: 58rpx;
					font-size: 28rpx;
					color: #202020;
					border-radius: 10rpx;
					border: 1px solid #cfd2d7;
				}
				.MatchReportLixText{
					height: 60rpx;
					line-height: 60rpx;
					font-size: 28rpx;
					color: #2f59fe;
					border-radius: 0;
					border: none;
				}
			}
		}
	}
	.uni-popup-message {
		padding: 0 !important;
		border-radius: 20rpx !important;
		box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08) !important;
		transform-origin: top;
		animation: messageFloat 0.4s cubic-bezier(0.215, 0.61, 0.355, 1) !important;
		max-width: 60% !important;
		margin: 0 auto !important;
		overflow: hidden !important;
		opacity: 0.97 !important;
		display: flex !important;
		align-items: center !important;
		justify-content: center !important;

		.uni-popup-message-text {
			font-size: 28rpx !important;
			color: #a13c1d9c !important;
			font-weight: 500 !important;
			letter-spacing: 0.5rpx !important;
			line-height: 1.5 !important;
			text-align: center !important;
			white-space: nowrap !important;
			overflow: hidden !important;
			text-overflow: ellipsis !important;
			margin: 0 0 0 -3rpx !important;
			width: 100% !important;
			display: flex !important;
			align-items: center !important;
			justify-content: center !important;
		}
	}
</style>
